﻿using Big_Data.Data_Base_Excel.Sante;
using Big_Data.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data
{
    public partial class Form3 : Form
    {
        public BackgroundWorker worker { get; set; }
        Excel_File excel = new Excel_File();


        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            List<string> lstFile = new List<string>();
            string path = Application.StartupPath+@"\File Base\";
            DirectoryInfo dirInfos = new DirectoryInfo(path);
            foreach (var folder in dirInfos.EnumerateDirectories())
            {
                TreeNode folderNode = new TreeNode(folder.Name);
                folderNode.ImageIndex = 0;
                folderNode.SelectedImageIndex = 0;
                foreach (var file in folder.EnumerateFiles())
                {
                    TreeNode fileNode = new TreeNode(file.Name);
                    fileNode.ImageIndex = 1;
                    fileNode.SelectedImageIndex = 1;
                    fileNode.Name = file.FullName;
                    folderNode.Nodes.Add(fileNode);
                }
                treeView1.Nodes.Add(folderNode);
            }
        }

        private void buttoImport_Click(object sender, EventArgs e)
        {
            try
            {
                excel.path = this.treeView1.SelectedNode.Name;

                if (!backgroundWorker1.IsBusy)
                {
                    backgroundWorker1.RunWorkerAsync();
                    this.buttoImport.Enabled = false;
                    this.buttonCancelImport.Enabled = true;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            worker = sender as BackgroundWorker;
            Add(worker, e);
        }

        public void Add(BackgroundWorker worker,DoWorkEventArgs e)
        {
            Import importSante = new Import();
            excel.ExcelOpen();
            foreach (var sheet in excel.ExcelScanNameSheet())
            {
                excel.OpenSheet(sheet);
                object[,] valueArray = excel.ExcelScanValueSheet();
                importSante.ImportDepenseSante(valueArray,worker,e);
            } 
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled)
                    this.labelErr.Text = "Annuler";
                else if (!(e.Error == null))
                {
                    this.labelErr.Text = "Erreur: " + e.Error.Message;
                    MessageBox.Show(e.Error.Message);
                }
                else
                {
                    this.labelprogBar.Text = "Done!";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                excel.CleanUp();
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
            this.labelprogBar.Text = e.ProgressPercentage.ToString() + "%";
        }

        private void buttonCancelImport_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.WorkerSupportsCancellation)
            {
                backgroundWorker1.CancelAsync();
                this.buttonCancelImport.Enabled = false;
                this.buttoImport.Enabled = true;
            }
        }


    }
}
